import store from "./store"

export default {
	//开启命名空间
	namespaced: true,

	//state数据
	state: () => ({
		isPlay: false, //播放状态
		songId: null, //音乐Id
		songUrl: '', //歌曲URL
		duration: 0, //音乐时长
		curTime: 0, //当前音乐的播放位置
		changeTime: 0, //指定播放时刻
		songName: '', //歌名
		singerName: '', //歌手名
		songPhoto: '', //歌手图片
		autoNext: true, //触发自动播放下一首
		songlist: [], //歌单的信息
		listIndex: 0, //当前歌曲在歌曲列表的位置
		song: {} ,//单个音乐
		songer:{} ,//歌手
		tuijianSongs:[] //推荐歌曲
	}),

	//方法
	mutations: {
		setIsPlay: (state, isPlay) => {
			state.isPlay = isPlay
			uni.setStorage('isPlay', JSON.stringify(state.isPlay))
		},
		setSongId: (state, songId) => {
			state.songId = songId
			uni.setStorage('songId', JSON.stringify(state.songId))
		},
		setSongUrl: (state, songUrl) => {
			state.songUrl = songUrl
			uni.setStorage('songUrl', JSON.stringify(state.songUrl))
		},
		setDuration: (state, duration) => {
			state.duration = duration
			uni.setStorage('duration', JSON.stringify(state.duration))
		},
		setCurTime: (state, curTime) => {
			state.curTime = curTime
			uni.setStorage('curTime', JSON.stringify(state.curTime))
		},
		setChangeTime: (state, changeTime) => {
			state.changeTime = changeTime
			uni.setStorage('changeTime', JSON.stringify(state.changeTime))
		},
		setSongName: (state, songName) => {
			state.songName = songName
			uni.setStorage('songName', JSON.stringify(state.songName))
		},
		setSingerName: (state, singerName) => {
			state.singerName = singerName
			uni.setStorage('singerName', JSON.stringify(state.singerName))
		},
		setsongPhoto: (state, songPhoto) => {
			state.songPhoto = songPhoto
			uni.setStorage('songPhoto', JSON.stringify(state.songPhoto))
		},
		setAutoNext: (state, autoNext) => {
			state.autoNext = autoNext
			uni.setStorage('autoNext', JSON.stringify(state.autoNext))
		},
		setSongList: (state, songList) => {
			state.songlist = songList
			uni.setStorage('songList', JSON.stringify(state.songlist))
		},
		settuijianSongs: (state, tuijianSongs) => {
			state.tuijianSongs = tuijianSongs
			uni.setStorage('tuijianSongs', JSON.stringify(state.tuijianSongs))
		},
		addSongList: (state, song) => {
			if (song != null){
				let flag = false;
				for(let i = 0;i<state.songlist.length;i++){
					if(song.songId===state.songlist[i].songId){
						flag = true
						break
					}
				}
				if(!flag){
					state.songlist.push(song)
				}
			}
			console.log(state.songlist);

			for (let i = 0; i < state.songlist.length; i++) {
				if (state.songlist[i] === null) {
					state.songlist.splice(i)
				}
			}
		},
		setListIndex: (state, listIndex) => {
			state.listIndex = listIndex
			uni.setStorage('listIndex', JSON.stringify(state.listIndex))
		},
		setSonger: (state, songer) => {
			state.songer = songer
			uni.setStorage('songer', JSON.stringify(state.songer))
		},
		setSong(state, song) {
			state.song = song
			if (song != null)
				// this.addSongList(state,song)
				this.commit('song/addSongList',song);
			state.isPlay = true
			uni.setStorage('song', JSON.stringify(state.song))
		}
	},

	//数据包装器
	getters: {
		isPlay: state => state.isPlay,
		songId: state => {
			let songId = state.songId
			if (!songId) {
				songId = JSON.parse(uni.getStorageSync('songId') || null)
			}
			return songId
		},
		songUrl: state => {
			let songUrl = state.songUrl
			if (!songUrl) {
				songUrl = JSON.parse(uni.getStorageSync('songUrl') || null)
			}
			return songUrl
		},
		duration: state => {
			let duration = state.duration
			if (!duration) {
				duration = JSON.parse(uni.getStorageSync('duration') || 0)
			}
			return duration
		},
		curTime: state => {
			let curTime = state.curTime
			if (!curTime) {
				curTime = JSON.parse(uni.getStorageSync('curTime') || 0)
			}
			return curTime
		},
		changeTime: state => {
			let changeTime = state.changeTime
			if (!changeTime) {
				changeTime = JSON.parse(uni.getStorageSync('changeTime') || 0)
			}
			return changeTime
		},
		songName: state => {
			let songName = state.songName
			if (!songName) {
				songName = JSON.parse(uni.getStorageSync('songName') || null)
			}
			return songName
		},
		singerName: state => {
			let singerName = state.singerName
			if (!singerName) {
				singerName = JSON.parse(uni.getStorageSync('singerName') || null)
			}
			return singerName
		},
		songPhoto: state => {
			let songPhoto = state.songPhoto
			if (!songPhoto) {
				songPhoto = JSON.parse(uni.getStorageSync('songPhoto'))
			}
			return songPhoto
		},
		autoNext: state => {
			let autoNext = state.autoNext
			if (!autoNext) {
				autoNext = JSON.parse(uni.getStorageSync('autoNext') || null)
			}
			return autoNext
		},
		songlist: state => {
			let songlist = state.songlist
			if (JSON.stringify(songlist) === '{}') {
				songlist = JSON.parse(uni.getStorageSync('songlist') || null)
			}
			return songlist
		},
		listIndex: state => {
			let listIndex = state.listIndex
			if (!listIndex) {
				listIndex = JSON.parse(uni.getStorageSync('listIndex') || null)
			}
			return listIndex
		},
		song: state => {
			let song = state.song
			if (!song) {
				song = JSON.parse(uni.getStorageSync('song') || null)
			}
			return song
		}
	}
}
